package com.ruibo.tax.mq.receive;


import com.ibm.mq.MQException;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ruibo.tax.constants.ReceiveDataPackageConstants;
import com.ruibo.tax.constants.SendDataPackageConstants;
import com.ruibo.tax.model.HBJHXKZJBXX;
import com.ruibo.tax.model.base.PackageHead;
import com.ruibo.tax.model.base.XMLEncodeModel;
import com.ruibo.tax.model.dz.SSDZXX;
import com.ruibo.tax.mq.send.ReconciliationSendZIPMessage;
import com.ruibo.tax.repository.TaxLogRepository;
import com.ruibo.tax.service.*;
import com.ruibo.tax.utils.XMLUtils;
import com.ruibo.tax.utils.ZipUtils;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.*;

import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.concurrent.TimeUnit;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;


@Slf4j
@Component
public class QLSW2HBTask {


    //    税务对环保的队列
    @Resource(name = "QLSW2HB")
    MQQueue sw2hbMQQueue;


    @Autowired
    private XMLUtils xmlUtils;

    /**
     * 税务复核提请信息
     */
    @Autowired
    private FHTQXXService fhtqxxService;
    /**
     * 涉税企业
     */
    @Autowired
    private SWSSQYService swssqyService;
    /**
     * 税务基础税源信息
     */
    @Autowired
    private SWJCSYService swjcsyService;
    /**
     * 税务涉税企业大气水污染物信息
     */
    @Autowired
    private SSDQSWRService ssdqswrService;
    /**
     * 税务涉税企业固体废物信息
     */
    @Autowired
    private SSGTFWService ssgtfwService;
    /**
     * 税务涉税企业噪声信息
     */
    @Autowired
    private SSZSXXService sszsxxService;
    /**
     * 税务产排污系数基础信息-
     */
    @Autowired
    private CPWXSJCService cpwxsjcService;
    /**
     * 环境保护税纳税申报数据信息-A类
     */
    @Autowired
    private SWNSSBAService swnssbaService;
    /**
     * 大气污染物纳税申报计算信息
     */
    @Autowired
    private DQWRNSSBService dqwrnssbService;
    /**
     * 水污染纳税申报计算信息
     */
    @Autowired
    private SWRNSSBService swrnssbService;
    /**
     * 固体废物污染纳税申报计算信息
     */
    @Autowired
    private GTFWNSSBService gtfwnssbService;
    /**
     * 噪声污染纳税申报计算信息
     */
    @Autowired
    private ZSNSSBService zsnssbService;

    /**
     * 减免明细纳税申报信息
     */
    @Autowired
    private JMMXNSSBService jmmxnssbService;
    /**
     * 环境保护税纳税申报数据信息-B类
     */
    @Autowired
    private SWNSSBBService swnssbbService;
    /**
     * 环境保护税入库数据信息
     */
    @Autowired
    private SWRKXXService swrkxxService;

    @Autowired
    private ReconciliationSendZIPMessage reconciliationSendZIPMessage;

    @Autowired
    private TaxLogService taxLogService;


    /**
     * 接受环保发送到税务的数据  正式环境
     */
    public void receiveZIPMsgFromQLSW2HB() {
        try {
            // 获取对账队列中所有数据
            int currentDepth = sw2hbMQQueue.getCurrentDepth();
            for (int i = 0; i < currentDepth; i++) {
                MQMessage retrievedMessage = new MQMessage();
                sw2hbMQQueue.get(retrievedMessage);
                int messageLength = retrievedMessage.getMessageLength();
                log.info("消息的大小为：{}", messageLength);
                byte[] msgData = new byte[messageLength];
                retrievedMessage.readFully(msgData);
                /**
                 *  目前只能先把数据 读到磁盘上然后  再从磁盘读取 进行操作
                 */
                RandomAccessFile accessFile = new RandomAccessFile(new File("C:/a.zip"), "rw");
                FileChannel channel = accessFile.getChannel();
                channel.write(ByteBuffer.wrap(msgData));
                channel.close();
                accessFile.close();
                // 读取并处理压缩文件
                readZipFile("C:/a.zip");
                TimeUnit.SECONDS.sleep(1);

            }
        } catch (Exception e) {
            log.error(e.toString());
            e.printStackTrace();
        }
    }

    /**
     * 读取并处理压缩文件
     *
     * @param filePath
     * @throws Exception
     */
    public void readZipFile(String filePath) {
        try {
            ZipFile zf = new ZipFile(filePath);
            Enumeration zipEnum = zf.entries();
            while (zipEnum.hasMoreElements()) {
                ZipEntry ze = (ZipEntry) zipEnum.nextElement();
                if (ze.isDirectory()) {
                    //为空的文件夹什么都不做
                } else {
                    if (ze.getSize() > 0) {
                        BufferedReader br = new BufferedReader(
                                new InputStreamReader(zf.getInputStream(ze), "GBK"));
                        String line;
                        while ((line = br.readLine()) != null) {
                            // 处理接受的数据
                            JAXBContext jaxbContext = JAXBContext.newInstance(XMLEncodeModel.class);
                            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
                            StringReader stringReader = new StringReader(line);
                            XMLEncodeModel xmlEncodeModel = (XMLEncodeModel) jaxbUnmarshaller.unmarshal(stringReader);
                            //1、 生成文件保存
                            xmlUtils.creteFile(line, xmlEncodeModel);
                            //2、保存数据
                            // 获取heads。根据heads不同的类型 创建不同的数据解析模型
                            PackageHead heads = xmlEncodeModel.getHeads();

                            String sjblx = heads.getSJBLX();

                            String data = line;
                            //税务复核提请信息
                            if (ReceiveDataPackageConstants.FHTQXX.equals(sjblx)) {
                                fhtqxxService.processingData(data, heads);
                                //税务涉税企业基本信息
                            } else if (ReceiveDataPackageConstants.SWSSQY.equals(sjblx)) {
                                swssqyService.processingData(data, heads);
                                //税务基础税源信息
                            } else if (ReceiveDataPackageConstants.SWJCSY.equals(sjblx)) {
                                swjcsyService.processingData(data, heads);
                                //税务涉税企业大气水污染物信息
                            } else if (ReceiveDataPackageConstants.SSDQSWR.equals(sjblx)) {
                                ssdqswrService.processingData(data, heads);
                                //税务涉税企业固体废物信息
                            } else if (ReceiveDataPackageConstants.SSGTFW.equals(sjblx)) {
                                ssgtfwService.processingData(data, heads);
                                //税务涉税企业噪声信息
                            } else if (ReceiveDataPackageConstants.SSZSXX.equals(sjblx)) {
                                sszsxxService.processingData(data, heads);
                                //税务产排污系数基础信息
                            } else if (ReceiveDataPackageConstants.CPWXSJC.equals(sjblx)) {
                                cpwxsjcService.processingData(data, heads);
                                //环境保护税纳税申报数据信息-A类
                            } else if (ReceiveDataPackageConstants.SWNSSBA.equals(sjblx)) {
                                swnssbaService.processingData(data, heads);
                                //大气污染物纳税申报计算信息
                            } else if (ReceiveDataPackageConstants.DQWRNSSB.equals(sjblx)) {
                                dqwrnssbService.processingData(data, heads);
                                //水污染纳税申报计算信息
                            } else if (ReceiveDataPackageConstants.SWRNSSB.equals(sjblx)) {
                                swrnssbService.processingData(data, heads);
                                //固体废物污染纳税申报计算信息
                            } else if (ReceiveDataPackageConstants.GTFWNSSB.equals(sjblx)) {
                                gtfwnssbService.processingData(data, heads);
                                //噪声污染纳税申报计算信息
                            } else if (ReceiveDataPackageConstants.ZSNSSB.equals(sjblx)) {
                                zsnssbService.processingData(data, heads);
                                //减免明细纳税申报信息
                            } else if (ReceiveDataPackageConstants.JMMXNSSB.equals(sjblx)) {
                                jmmxnssbService.processingData(data, heads);
                                //环境保护税纳税申报数据信息-B类
                            } else if (ReceiveDataPackageConstants.SWNSSBB.equals(sjblx)) {
                                swnssbbService.processingData(data, heads);
                                //环境保护税入库数据信息
                            } else if (ReceiveDataPackageConstants.SWRKXX.equals(sjblx)) {
                                swrkxxService.processingData(data, heads);
                            }
                            // 发送实时对账信息
                            reconciliationSendZIPMessage.sendRealTimeDZMsg(heads);
                            log.info("处理数据成功");
                        }
                        br.close();
                    }
                }
            }
            zf.close();
        } catch (Exception e) {
            log.error("读取压缩文件失败,{}", e.toString());
        }

    }
}
